<?php
namespace Admin\controller;
use \Admin\Controller\AdminController;
class AduserController extends AdminController
{

	//用户列表页
	public function index()
	{
		$where = '';
		$model =  M('adminusers');

		if(!empty($_GET)){
			$name = urldecode(I('get.name'));
			$where['name'] = array('like','%'.$name.'%');
			$map['name'] = $name; 
			$this->assign('where',$name);
		}

		$count = $model->where($where)->count();
		$Page = new \Think\Page($count,5);
		$Page->lastSuffix = false;
		//定制分页样式
		$Page->setConfig('header','共有 %TOTAL_ROW% 个用户');
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('first','首页');
		$Page->setConfig('last','尾页');

		//修改主题
		$Page->setConfig('theme',' <ul class="pagination"><li>%FIRST%</li> <li>%UP_PAGE%</li> <li>%LINK_PAGE%</li> <li>%DOWN_PAGE%</li> <li>%END%</li></ul>');

		foreach($map as $key=>$val) {
			$Page->parameter[$key] = urlencode($val);
		}

		$show = $Page->show();
		$data = $model->order('id asc')->limit($Page->firstRow.','.$Page->listRows)->where($where)->select();
		$this->assign('title','用户管理系统');
		$this->assign('list',$data);
		$this->assign('page',$show);
		$this->display('Aduser/index');
	}
	public function add()
	{
		
        
		$this->assign('title','用户添加');
		$this->display();
	}
	
	public function insert()
	{   
		$rules = array ( 
		    array('password','md5',3,'function') , // 对password字段在新增和编辑的时候使md5函数处理
		);
        $model = M('adminusers');
		if($model->auto($rules)->create()) {
			$result = $model->add();
			var_dump($result);
			if($result) {
				$this->success('数据添加成功！',U('index'));
				
			}else{
				$this->error('数据添加错误！');
			}
		}else{
			$this->error('数据有误！');
		}

	}

	/////////////////////////////////////////////////////

//////////////////////////////////////////////////////////
    public function upload(){
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize = 3145728 ;// 设置附件上传大小
		$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->rootPath = './Public/Uploads/'; // 设置附件上传根目录
		$upload->savePath = ''; // 设置附件上传（子）目录
        $upload->saveName = date('Ymd').mt_rand();
		$upload->uploadReplace = false;//存在同名文件是否是覆盖
        $upload->thumbRemoveOrigin = false; //生成缩略图后是否删除原图
        $upload->thumb = true; //是否需要对图片文件进行缩略图处理，默认为false
		// 上传文件
		$info = $upload->upload();
		var_dump($info);
		if(!$info) {// 上传错误提示错误信息
		$this->error('文件上传失败');
		}else{// 上传成功
		$this->success('上传成功！');
		$this->insert();
        
		}
		}


////////////////////////////////////////////////////////


public function edit()
	{
		$id = I('get.id');
           
		$data = M('adminusers')->find($id);
       // var_dump($data);
		if(is_null($data)){
			$this->redirect('index');
			return;
		}
		$this->assign('title','用户修改');
		$this->assign('list',$data);
		$this->display('edit');
	}
	public function save()
	{
		$model = M('adminusers');
		if($model->create()) {
			$result = $model->save();
			if($result) {
				$this->success('数据修改成功！',U('index'));
			}else{
				$this->error('数据修改错误！',U('edit',array('id'=>I('post.id'))));
			}
		}else{
			$this->error('数据有误！');
		}

	}
	public function del()
	{
		if(!IS_AJAX){
			$this->error('请用正确的方式提交',U('Student/index'));
			return;
		}
		//强制转换为整型，默认值为0
		$id = I('get.id/d',0);

		if(M('adminusers')->delete($id)){
			echo 1;
		}else{
			echo 0;
		}
	}


}